<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Controller\agent;



use App\Dao\AgentMoneyDao;
use App\Kernel\Util\AgentJwtInstance;
use App\Middleware\User\AgentMiddleware;
use App\Dao\AgentCashDao;
use App\Request\Agent\User\UserCashRequest;

use App\Service\Agent\MoneyService;
use App\Service\Agent\UserCashService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\Snowflake\IdGeneratorInterface;

/**
 * 提现控制器
 *
 * @AutoController()
 * @Middleware(AgentMiddleware::class)
 * @author 张昊(2396675557@qq.com)
 * @package App\Controller\admin
 */
class UserCashController extends Controller
{

    /**
     * @Inject
     * @var AgentCashDao
     */
    private $dao;

    /**
     * @Inject
     * @var UserCashService
     */
    private $user_cash_service;

    /**
     * @Inject
     * @var MoneyService
     */
    public $money_service;

    /**
     * 提现记录
     */
    public function get()
    {
        $result = $this->user_cash_service->list();

        $this->success($result);
    }

    /**
     * 代理商提现搜索
     */
    public function search()
    {
        $params = $this->request->all();

        $cash_list = $this->user_cash_service->getCashList($params);

        $this->success($cash_list);
    }


    /**
     * 新增提现
     *
     * @param UserCashRequest $request
     */
    public function create(UserCashRequest $request)
    {
        $params = $request->all();

        $user = AgentJwtInstance::instance()->build()->getAgent();
        if( !empty($user->safe_pwd) ) {
            //验证安全密码
            if( !password_verify( $params['safe_pwd'], $user->safe_pwd ) ) {
                $this->error('ORDER.REGISTER_FAIL');
            }
        } else {
            $this->error('ORDER.REGISTER_EMPTY');
        }

        $user_money = $this->money_service->getMoneyBalanceByAgent(agentId());

        if( $user_money < $params['amount'] ) {
            $this->error("USER.NOT_MONEY");
        }

        if( $params['amount']  < 200 ) {
            $this->error("USER.NOT_TWO_HUNDRED");
        }

        $order_no = (string)$this->container->get(IdGeneratorInterface::class)->generate();

        $create = $this->dao->insertGetId([
            'agent_id' => uid(),
            'amount' => $params['amount'],
            'order_no' => $order_no,
            'bank_name' => $params['bank_name'],
            'bank_account' => $params['bank_account'],
            'bank_realname' => $params['bank_realname'],
            'bank_idcard' => $params['bank_idcard'],
            'bank_phone' => $params['bank_phone'],
            'status' => $this->dao::CASH_IN,
            'start_time' => time(),
            'created_at' => time(),
            'updated_at' => time(),
        ]);

        $this->money_service->agent_money_dao->create([
            'status' => $this->money_service->agent_money_dao::STATUS_ENABLE,
            'user_id' => uid(),
            'type' => $this->money_service->agent_money_dao::CONSUME_TYPE,
            'source' => $this->money_service->agent_money_dao::SOURCE_CASH,
            'num' => $params['amount'],
            'created_at' => time(),
        ]);

        $this->money_service->updateMoneyAgentBalance( agentId() );

        !$create && $this->error();

        $this->success($create);
    }

    /**
     * 取消提现
     */
    public function delete()
    {
        $cash_id = (array)$this->request->input('cash_id', []);
        if ($cash_id === []) $this->error();

        $this->dao->delete(['in', $cash_id]);

        $this->success();
    }

    /**
     * 通过提现ID 获取提现数据
     */
    public function getUserCashById()
    {
        $cash_id = (int)$this->request->input('cash_id', 0);

        $result = $this->dao->first($cash_id,['id','order_no','amount','bank_name','bank_account','start_time','status','refusal_reason','processing_time']);

        $this->success($result);
    }
}